package versi.vitro.oaiserviceprovider.service.jena.helper;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class JenaHelperImplTest {

    private Mockery context;

    @BeforeMethod
    public void setUp() {
	context = new Mockery();
    }

    @Test
    public void testValidateRDFFormatWhenFormatIsValid() {
	JenaHelperImpl jenaHelper = new JenaHelperImpl();
	boolean isValid = jenaHelper.validateRDFFormat("RDF/XML-ABBREV");
	assert isValid;
    }

    @Test
    public void testValidateRDFFormatWhenFormatIsValidButNotStatic() {
	JenaHelperImpl jenaHelper = new JenaHelperImpl();
	boolean isValid = jenaHelper.validateRDFFormat("rdf/xml-abbrev".toUpperCase());
	assert isValid;
    }

    @Test
    public void testValidateRDFFormatWhenFormatIsInvalid() {
	JenaHelperImpl jenaHelper = new JenaHelperImpl();
	boolean isValid = jenaHelper.validateRDFFormat("XXXXX");
	assert !isValid;
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testPrintAllModelsSize() {

	final String modelName = "dummyModelName";
	final ExtendedIterator<String> extendedIterator = context.mock(ExtendedIterator.class);
	context.checking(new Expectations() {
	    {
		exactly(2).of(extendedIterator).hasNext();
		will(onConsecutiveCalls(returnValue(true), returnValue(false)));
		exactly(1).of(extendedIterator).next();
		will(onConsecutiveCalls(returnValue(modelName)));
	    }
	});

	final Model model = context.mock(Model.class);
	context.checking(new Expectations() {
	    {
		exactly(1).of(model).size();
		will(returnValue(0L));
	    }
	});

	final ModelMaker modelMaker = context.mock(ModelMaker.class);
	context.checking(new Expectations() {
	    {
		oneOf(modelMaker).listModels();
		will(returnValue(extendedIterator));
		exactly(1).of(modelMaker).getModel(with(any(String.class)));
		will(returnValue(model));
	    }
	});

	JenaHelperImpl jenaHelper = new JenaHelperImpl()
	{
	    protected boolean isDebugMode() {
		return true;
	    }
	};
	jenaHelper.printAllModelsSize(modelMaker);

	context.assertIsSatisfied();
    }

}
